跳到主要内容

Swift 范围

阐述

区间算符 ... 可以用来创建一个闭区间,或者一个单侧的区间;半开区间算符 ..< 可以用来创建一个不包含结尾的算符。

实例

let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack

for name in names[2...] {
print(name)
}
// Brian
// Jack


for name in names[...2] {
print(name)
}
// Anna
// Alex
// Brian

性质

相关内容

参考文献